/*
 * @Description: 
 * @Author: HKH
 * @Date: 2024-10-09 17:30:51
 * @LastEditors: HKH
 * @LastEditTime: 2024-10-09 17:34:56
 */


import java.util.Map;
import java.util.Set;
import java.util.Collections;
import java.util.Map.Entry;

import java.util.HashMap;

/**
 * Test01Map
 */
public class Test01Map {

  public static void main(String[] args) {
    // 创建HashMap对象
    Map<Integer, String> map = new HashMap<>();
    Collections
    // 给map添加元素
    map.put(1, "zs");
    map.put(2, "ls");
    map.put(3, "ww");
    map.put(1, "zl"); // zl 将 zs 覆盖掉了
    map.put(4, "5"); // zl 将 zs 覆盖掉了
    // 输出map
    System.out.println(map.size());
    System.out.println(map);
    // 判断 key value 是否存在
    System.out.println(map.containsKey(1)); // true
    System.out.println(map.containsKey(5)); // false
    System.out.println(map.containsValue(5)); // false
    System.out.println(map.containsValue("5")); // true
    // 根据key去查询value
    String name = map.get(1);
    System.out.println(name);
    // 根据key去删除键值对
    System.out.println(map);
    // map.remove(1);
    // map.clear();
    System.out.println(map);
    // 遍历
    // 1. 先获取key值 keySet();
    Set<Integer> set = map.keySet();
    System.out.println(set); // [1, 2, 3, 4]
    // 2.for size() \ for-each
    for (Integer integer : set) {
      // System.out.println(integer);
      // get(key)
      String name1 = map.get(integer);
      // 输出键值对
      System.out.println("id: " + integer + ",name: " + name1);
    }
    // 第二种遍历思路 entrySet
    Set<Entry<Integer, String>> entrySet = map.entrySet();
    for (Entry<Integer, String> entry : entrySet) {
      System.out.println(entry);
      Integer key = entry.getKey();
      String value = entry.getValue();
      System.out.println("id: " + key + ",name: " + value);
    }
  }
}